package com.yunyouyi.cpicture.infrastructure.common;

import com.yunyouyi.cpicture.infrastructure.exception.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private int code;//编码 1成功其他失败
    private T data;//数据
    private String msg; //错误信息

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 0;
        result.msg = "ok";
        return result;
    }

    public static <T> Result<T> success(T object){
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = 0;
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        Result<T> result = new Result<T>();
        result.code = errorCode.getCode();
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        result.code = 0;
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode,String msg) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        result.code = errorCode.getCode();
        return result;
    }

    public static <T> Result<T> error(int errorCode,String msg) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        result.code = errorCode;
        return result;
    }
   /* public Result(int code, T data) {
        this(code,data,"");
    }

    public  Result(ErrorCode errorCode) {
        this(errorCode.getCode(),null,errorCode.getMessage());
    }*/
}
